/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CategoryBeanLocal;
import ejb.ItemPurchasedBeanLocal;
import ejb.PurchaseBeanLocal;
import entities.Item;
import entities.ItemPurchased;
import entities.Purchase;
import entities.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.component.dialog.Dialog;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@SessionScoped
public class PurchaseManagedBean {

    private Purchase purchase;
    private List<ItemPurchased> cart;
    private Integer quantity;
    
    @EJB
    private PurchaseBeanLocal purchaseBean;
    @EJB
    private ItemPurchasedBeanLocal itemPurchasedBean;

    public PurchaseManagedBean() {
        purchase = new Purchase();
        cart = new ArrayList<ItemPurchased>();
        quantity = 1;
    }

    public Purchase getPurchase() {
        return purchase;
    }

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }

    public void increment() {
        if (quantity < 50) {
            quantity++;
        }
    }

    public void decrement() {
        if (quantity > 1) {
            quantity--;
        }
    }

    public String viewCart() {
        if (cart == null) {
            cart = new ArrayList<ItemPurchased>();
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.NEXT_VIEW, Constants.CART + Constants.SEPARATOR + 1);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.EXTRA_CART, cart);
        return "UPDATE";
    }

    public List<ItemPurchased> getCart() {
        return cart;
    }

    public void setCart(List<ItemPurchased> cart) {
        this.cart = cart;
    }

    public String addCart(Item item) {
        if (cart == null) {
            cart = new ArrayList<ItemPurchased>();
        }
        if (purchase == null) {
            purchase = new Purchase();
        }
        // Item item = (Item) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("item");
        ItemPurchased itemPurchased = new ItemPurchased();
        itemPurchased.setItem(item);
        itemPurchased.setPurchase(purchase);
        itemPurchased.setPrice(item.getPrice());
        itemPurchased.setQuantity(quantity);

        

        boolean add = true;
        for (ItemPurchased it : cart) {
            if (it.getItem().getIdItem().equals(itemPurchased.getItem().getIdItem())) {
                it.setQuantity(it.getQuantity() + itemPurchased.getQuantity());
                add = false;
                break;
            }
        }
        if (add) {
            cart.add(itemPurchased);
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.NEXT_VIEW, Constants.CATEGORY + Constants.SEPARATOR + item.getIdCategory().getIdCategory());

        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Añadido " + quantity + " unid. de " + item.getName(), ""));
        quantity = 1;

        return "UPDATE";
    }

    public void deleteCart(ItemPurchased itemPurchased) {
        
        Iterator<ItemPurchased> iterator = cart.iterator();
        while (iterator.hasNext()) {
            ItemPurchased it = iterator.next();
            if (it.getItem().getIdItem().equals(itemPurchased.getItem().getIdItem())) {
                iterator.remove();
                break;
                
            }
        }
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
    
    public Double total(){
        Double res=0.0;
        for(ItemPurchased p:cart){
            res+=p.getPrice()*p.getQuantity();
        }
        return Math.rint((res)*100)/100;
    }
    
    public String doShopping(){
        //purchase.setUser();
        User user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.ACTUAL_USER);
        purchase.setUser(user);
        purchase.setPurchaseDate(new Date());
        purchase.setItemPurchase(cart);
        purchaseBean.addPurchase(purchase);
        cart=null;
        purchase=null;
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(Constants.NEXT_VIEW);
//        <p:dialog id="modalDialog" header="Modal Dialog" widgetVar="dlg2" modal="true" height="100">  
//    <h:outputText value="This is a Modal Dialog." />  
//</p:dialog> 
//        Dialog d = new Dialog();
//        d.setId("didShop");
//        d.setHeader("Su compra se ha realizado correctamente.");
//        d.setModal(true);
//        d.setHeight("10");
//        d.setWidgetVar("shopped");
//        falta shopped.show()
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Compra realizada correctamente!", "Muchas gracias"));

        return "UPDATE";
    }

    public PurchaseBeanLocal getPurchaseBean() {
        return purchaseBean;
    }

    public void setPurchaseBean(PurchaseBeanLocal purchaseBean) {
        this.purchaseBean = purchaseBean;
    }

    public ItemPurchasedBeanLocal getItemPurchasedBean() {
        return itemPurchasedBean;
    }

    public void setItemPurchasedBean(ItemPurchasedBeanLocal itemPurchasedBean) {
        this.itemPurchasedBean = itemPurchasedBean;
    }
    
    public Boolean canPurchased(){
        return cart!=null && purchase!=null && !cart.isEmpty();
    }
    
    public List<Purchase> purchases(){
        User user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.ACTUAL_USER);
        
        List<Purchase> purchases = purchaseBean.findByUser(user);
        return purchases;
    }
}
